<?php


namespace App\Api\Controller;

use App\Api\Model\ResumeInfoCompare;
use App\Facades\UrlCrypt;
use App\Web\Model\Attach;
use App\Web\Model\Resume as ResumeModel;
use EchoPHP\Facades\Db;
use EchoPHP\Http\Request;

class ResumeDiff extends BaseController {
    public function getResumeDiff(Request $request) {
        $rpokey = $request->getParam('rpokey');
        if (empty($rpokey) || $rpokey != '2B34DEBF-F865-1AE0-67B3-9F6CFA10DD06') {
            exit();
        }
        echo "<pre/>";
        $resumeId = $request->getParam('resumeid');
        $sql = 'select taskid,createdate from UploadResume where resumeid = ?';
        $var = [$resumeId];
        $result = Db::selectOne($sql, $var);
        echo $result['taskid'] . '    ' . $result['createdate'] . '      ';

        $taskId = $result['taskid'];
        $resumeCompare = new ResumeInfoCompare();
        $result = $resumeCompare->compareDiff($resumeId, $taskId);

        $resumeDiff = $result['diffArr'];
        $token = UrlCrypt::generateToken([$resumeId]);
        $href = 'http://'.config('app.url').'/resumepage?resumeid=' . $resumeId . '&token=' . $token . "\n";

        echo "<h3><a href=".$href." target='_blank'>简历详情页</a></h3>";
        echo "\n";

        //获取简历附件列表
        $resumeattach = (new ResumeModel())->showAttach($resumeId);
        if (!$resumeattach) {
            var_dump(['attachList' => []]);
        } else {
            $attachids = array_column($resumeattach, 'attachid');
            $attachInfo = (new Attach())->batchGetAttach($attachids, 'attachid,attachname');
            $attach = [];
            foreach ($attachInfo as $item) {
                $attach[$item['attachid']] = $item['attachname'];
            }
            foreach ($resumeattach as $k => $v) {
                $resumeattach[$k]['attachname'] = $attach[$v['attachid']];
                unset($resumeattach[$k]['attachid']);
            }

            echo "<h3>简历附件:\n";
            echo "<div>";
            foreach ($resumeattach as $attach) {
                echo "<p> attachid: " . $attach['resumeattachid'] . "<\t>" . "username: " . $attach['username'] . "<\t>" . "attachname: " . $attach['attachname'];
                $href = 'http://51rpoapi.51job.com/Login/downloadResumeAttach?resumeattachid='.$attach['resumeattachid'];
                echo "<a href=" . $href . ">下载附件</a>";
            }
            echo "</div></h3>";
//            var_dump(['attachList' => $resumeattach]);
        }

        $itemArr = [];
        $itemArr[] = array('fieldName' => 'cname', 'Title' => '姓名');
        $itemArr[] = array('fieldName' => 'gender', 'Title' => '性别');
        $itemArr[] = array('fieldName' => 'birthday', 'Title' => '生日');
        $itemArr[] = array('fieldName' => 'workyear', 'Title' => '开始工作年份');
        $itemArr[] = array('fieldName' => 'phone', 'Title' => '手机');
        $itemArr[] = array('fieldName' => 'email', 'Title' => '邮箱');
        $itemArr[] = array('fieldName' => 'situation', 'Title' => '求职状态');
        $itemArr[] = array('fieldName' => 'area', 'Title' => '居住地');
        $itemArr[] = array('fieldName' => 'company', 'Title' => '当前公司');
        $itemArr[] = array('fieldName' => 'degree', 'Title' => '最高学历');
        $itemArr[] = array('fieldName' => 'major', 'Title' => '最高专业');
        $itemArr[] = array('fieldName' => 'school', 'Title' => '最高学校');

        $itemArr[] = array('fieldName' => 'salaryfrom', 'Title' => '薪资起始');
        $itemArr[] = array('fieldName' => 'salaryto', 'Title' => '薪资结束');
        $itemArr[] = array('fieldName' => 'currsalaryfrom', 'Title' => '当前薪资起始');
        $itemArr[] = array('fieldName' => 'currsalaryto', 'Title' => '当前薪资结束');
        $itemArr[] = array('fieldName' => 'expectsalaryfrom', 'Title' => '期望薪资起始');
        $itemArr[] = array('fieldName' => 'expectsalaryto', 'Title' => '期望薪资结束');
        $itemArr[] = array('fieldName' => 'expectarea', 'Title' => '期望工作地');
        $itemArr[] = array('fieldName' => 'position', 'Title' => '职能/职位');
        $itemArr[] = array('fieldName' => 'workindustry', 'Title' => '行业');
        $itemArr[] = array('fieldName' => 'entrytime', 'Title' => '到岗时间');
        $itemArr[] = array('fieldName' => 'jobterm', 'Title' => '工作类型'); //seektype
        $itemArr[] = array('fieldName' => 'selfintro', 'Title' => '自我评价');


        foreach ($itemArr as $item) {
            $itemName = $item['fieldName'];
            if (isset($resumeDiff[$itemName])) {
                echo '【' . $item['Title'] . '】原为：' . $resumeDiff[$itemName][$itemName][$itemName . "_before"] . '  ';
                if (in_array($itemName, array('selfintro'))) {
                    echo '<br/>';
                }
                echo '改为：' . $resumeDiff[$itemName][$itemName][$itemName . "_after"];
                echo '<br/>';
                unset($resumeDiff[$itemName]);
            }
        }

        unset($resumeDiff['mobilephone']);
        unset($resumeDiff['expsalaryfrom']);
        unset($resumeDiff['expsalaryto']);
        unset($resumeDiff['mobilephone']);

        if (isset($resumeDiff['workinfo']) && count($resumeDiff['workinfo']) > 0) {
            echo '工作经历有变动';
            echo '<br/>';
        }
        unset($resumeDiff['workinfo']);

        if (isset($resumeDiff['projectinfo']) && count($resumeDiff['projectinfo']) > 0) {
            echo '项目经验有变动';
            echo '<br/>';
        }
        unset($resumeDiff['projectinfo']);
        if (isset($resumeDiff['educationinfo']) && count($resumeDiff['educationinfo']) > 0) {
            echo '教育经历有变动';
            echo '<br/>';
        }
        unset($resumeDiff['educationinfo']);

        if (isset($resumeDiff['skillinfo']) && count($resumeDiff['skillinfo']) > 0) {
            echo 'skillinfo有变动';
            echo '<br/>';
        }
        unset($resumeDiff['skillinfo']);

        if (isset($resumeDiff['nofocuse']) && count($resumeDiff['nofocuse']) > 0) {
            echo 'nofocuse有变动';
            echo '<br/>';
        }
        unset($resumeDiff['nofocuse']);

        if (isset($resumeDiff['awardinfo']) && count($resumeDiff['awardinfo']) > 0) {
            echo 'awardinfo有变动';
            echo '<br/>';
        }
        unset($resumeDiff['awardinfo']);

        if (isset($resumeDiff['otherinfo']) && count($resumeDiff['otherinfo']) > 0) {
            echo 'otherinfo有变动';
            echo '<br/>';
        }
        unset($resumeDiff['otherinfo']);


        echo '<br/><br/>';
        var_dump($resumeDiff);

        echo '<textarea cols="100" rows="20" title="原始简历">' . $result['dataBefore'] . '</textarea>';
        echo '   <textarea cols="100" rows="20" title="最新简历">' . $result['resumeNew'] . '</textarea>';
        echo '<br/><br/><br/><br/>';
        echo '<textarea cols="100" rows="20" title="文件内容">' . $result['fileStr'] . '</textarea>';
        echo '   <textarea cols="100" rows="20" title="机器学习">' . $result['mlStr'] . '</textarea>';

        return;
    }

    /**
     * 获取未编码字典信息
     * @param Request $request
     * @throws \Exception
     */
    public function getUncodeDict(Request $request) {
        $month = $request->getParam('month'); //202004
        $dictName = $request->getParam('dictname'); //expectarea
        if (empty($month) || empty($dictName)) {
            echo '参数不能为空';
            return;
        }
        $filePath = config('app.resume_machine_learning.dict') . '/' . $month . '/' . $dictName . '.txt';
        if (!file_exists($filePath)) {
            echo '文件不存在';
            return;
        }

        return response()->download($filePath, $month . $dictName . '.txt');
    }
}